package com.louis.web.controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.NativeWebRequest;

import com.louis.util.ChangeCaseUtil;
import com.louis.util.StringUtil;

public class ListController<T> extends AbstractController<T> {

	private String	listView;
	private int		currentPage	= 0;
	private int		pageSize	= 0;

	public ListController() {
		String name = ChangeCaseUtil.firstCharacterLowerCase(getClazz().getSimpleName());
		this.listView = name + "/" + name + "list";
	}

	@RequestMapping(value = { "/list" + SUFFIX, "/list/{currentPage}" + SUFFIX, "/list/{pageSize}/{currentPage}" + SUFFIX })
	public String list(NativeWebRequest nativeWebRequest, ModelMap model) {
		setNativeWebRequest(nativeWebRequest);
		dataPrepare(model);
		return listView;
	}

	public int getCurrentPage() {
		String parameter = getRequest().getParameter("currentPage");
		if (!StringUtil.hasText(parameter)) {
			parameter = getPathVariable("currentPage");
		}
		if (StringUtil.hasText(parameter)) {
			this.currentPage = Integer.parseInt(parameter);
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		String parameter = getRequest().getParameter("pageSize");
		if (!StringUtil.hasText(parameter)) {
			parameter = getPathVariable("pageSize");
		}
		if (StringUtil.hasText(parameter)) {
			this.pageSize = Integer.parseInt(parameter);
		}
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
}
